using System.Collections;
using UnityEngine;

public class FollowingCamera : MonoBehaviour
{
	public static FollowingCamera instance;

	public GameObject player;

	private Transform target;

	private WaitForEndOfFrame wfef;

	private float targetSize;

	public Vector3 offset;

	public bool follow_x;

	public bool follow_y;

	public bool follow_z;

	private Camera cam;

	[HideInInspector]
	public Bounds camBound;

	[HideInInspector]
	public Bounds lodBound;

	[HideInInspector]
	public Bounds lod0Bound;
    private Vector3 initialPosition; // 摄像机初始位置
    private bool isShaking = false; // 是否正在震动
	public Camera Cam
	{
		get
		{
			return cam;
		}
	}

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		else
		{
			Object.Destroy(base.gameObject);
		}
		cam = GetComponent<Camera>();
		initialPosition = transform.localPosition; // 保存初始位置
	}

	private void Start()
	{
		target = player.transform;
		wfef = new WaitForEndOfFrame();
		targetSize = Values.Instance.InitialCameraSize;
	}

	private void Update()
	{
		if (!(target != null))
		{
			return;
		}
		 if (!isShaking) // 如果没有震动，正常跟随
        {
            if (target.position + offset != base.transform.position)
            {
                Vector3 position = base.transform.position;
                if (follow_x)
                {
                    position.x = target.position.x + offset.x;
                }
                if (follow_y)
                {
                    position.y = target.position.y + offset.y;
                }
                if (follow_z)
                {
                    position.z = target.position.z + offset.z;
                }
                base.transform.position = position;
            }
        }
		if (cam.orthographicSize != targetSize)
		{
			cam.orthographicSize = Mathf.MoveTowards(cam.orthographicSize, targetSize, 8f * Time.deltaTime);
		}
		OrthographicBounds();
	}

	public Vector3 GetPositionAt2D()
	{
		Vector3 position = base.transform.position;
		position.z = 0f;
		return position;
	}

	public void OrthographicBounds()
	{
		float num = (float)Screen.width / (float)Screen.height;
		float num2 = cam.orthographicSize * 2f;
		camBound = new Bounds(cam.transform.position, new Vector3(num2 * num, num2, 0f));
	}

	public bool IsInsideOfCamera2D(Vector3 pos)
	{
		return !(pos.x > camBound.max.x) && !(pos.x < camBound.min.x) && !(pos.y > camBound.max.y) && !(pos.y < camBound.min.y);
	}

	public void SetTarget(GameObject player)
	{
		target = player.transform;
		this.player = player;
	}

	public void SetTargetCameraSize(float targetSize)
	{
		this.targetSize = targetSize;
	}

	public void PlayCollisionAnim()
	{
		StartCoroutine(CollisionAnimRoutine());
	}

	private IEnumerator CollisionAnimRoutine()
	{
		SetTargetCameraSize(Values.Instance.MaxCameraSize);
		yield return new WaitForSeconds(Values.Instance.DieAnimDuration * 2f / 3f);
		SetTargetCameraSize(Values.Instance.InitialCameraSize);
	}
	 public void ShakeCamera(float duration, float magnitude)
    {
		initialPosition = transform.localPosition; // 保存初始位置
        if (!isShaking)
        {
            StartCoroutine(ShakeRoutine(duration, magnitude));
        }
    }

    private IEnumerator ShakeRoutine(float duration, float magnitude)
    {
        isShaking = true;
        float elapsed = 0f;

        while (elapsed < duration)
        {
            Vector3 randomOffset = Random.insideUnitSphere * magnitude;
            randomOffset.z = 0; // 保持 Z 轴不变
            transform.localPosition = initialPosition + randomOffset;

            elapsed += Time.deltaTime;
            yield return null;
        }

        transform.localPosition = initialPosition; // 恢复初始位置
        isShaking = false;
    }
}
